<h3 id="问题：求 100 之内的素数"><a href="#问题：求 100 之内的素数">问题：求 100 之内的素数</a></h3><h5 id="什么是素数？">  什么是素数？</h5><p>  素数又称质数，一个大于 1 的自然数，除了 1 和它自身外，不能被其他自然数整除的数叫做质数；否则称为合数。</p>
<h5 id="如何判断一个数是不是素数？">  如何判断一个数是不是素数？</h5><p>  在一般领域，对正整数 n，如果用 2 到这个数的平方根<br>之间的所有整数去除，均无法整除，则 n 为质数。<br>  质数大于等于 2 不能被它本身和 1 以外的数整除</p>
<p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 判断一个自然数是否是质数
 * <span class="hljs-doctag">@params</span>: 自然数
 * <span class="hljs-doctag">@retrun</span>: boolean(true:是质数，false:不是质数)
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">isPrime</span>(<span class="hljs-params">num:number</span>):boolean{
    <span class="hljs-keyword">let</span> numSqrt=<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">sqrt</span>(num);
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>;i&lt;numSqrt+<span class="hljs-number">1</span>;i++){
        <span class="hljs-keyword">if</span>(num%i == <span class="hljs-number">0</span>){
            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 判断两个数字(包含n1，不包含n2)之间的质数有哪些，并返回这两个数字之间的所有质数组成的列表
 * <span class="hljs-doctag">@params</span>: (n1,n2) 都是大于1的自然数，n1:查找大于等于n1的质数，n2：查找小于n2的质数
 * <span class="hljs-doctag">@retrun</span>: 列表
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">rangeNumPrime</span>(<span class="hljs-params">n1:number,n2:number</span>):number[]{
    <span class="hljs-keyword">if</span>(n1&lt;<span class="hljs-number">2</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">"素数范围是大于1的自然数"</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">if</span>(n1&gt;=n2){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">"第二个参数必须大于第一个参数"</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">let</span> primeNums=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> num =n1;num&lt;n2;num++){
        <span class="hljs-keyword">if</span>(<span class="hljs-title function_">isPrime</span>(num)){
            primeNums.<span class="hljs-title function_">push</span>(num)
        }
    }
    <span class="hljs-keyword">return</span> primeNums
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`100之内的素数有：<span class="hljs-subst">${rangeNumPrime(<span class="hljs-number">2</span>,<span class="hljs-number">100</span>).toString()}</span>`</span>)
<span class="hljs-comment">// 100之内的素数有：3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：对10个数进行排序"><a href="#问题：对10个数进行排序">问题：对10个数进行排序</a></h3><p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 插入排序
 * <span class="hljs-doctag">@params</span>: number[]
 * <span class="hljs-doctag">@retrun</span>: number[]
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">insertionSortAsc</span>(<span class="hljs-params">arr:number[]</span>):number[] {
    <span class="hljs-keyword">if</span> (!<span class="hljs-title class_">Array</span>.<span class="hljs-title function_">isArray</span>(arr)) {
        <span class="hljs-keyword">throw</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">"param must be a Array"</span>);
    }
    <span class="hljs-keyword">let</span> _arr = arr.<span class="hljs-title function_">concat</span>();
    <span class="hljs-keyword">let</span> arrLength = _arr.<span class="hljs-property">length</span>;
    <span class="hljs-keyword">if</span> (arrLength &lt; <span class="hljs-number">2</span>) {
        <span class="hljs-keyword">return</span> _arr;
    }
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">1</span>;i&lt;arrLength;i++){
        <span class="hljs-keyword">let</span> current=_arr[i];
        <span class="hljs-keyword">let</span> j=i-<span class="hljs-number">1</span>;
        <span class="hljs-keyword">for</span>(;j&gt;-<span class="hljs-number">1</span>;j--){
            <span class="hljs-comment">//从右向左找出第一个小于等于当前元素的元素</span>
            <span class="hljs-keyword">if</span>(current&lt;_arr[j]){
                <span class="hljs-keyword">continue</span>;
            }
            <span class="hljs-keyword">break</span>;
        }
        <span class="hljs-comment">//如果从右向左第一个小于等于当前元素的位置就在当前元素的前一位，就不用再做处理了</span>
        <span class="hljs-keyword">if</span>(j!=i-<span class="hljs-number">1</span>){
            <span class="hljs-comment">//将当前元素从数组取出，直接插入到从右向左第一个小于等于当前元素的位置后面</span>
            _arr.<span class="hljs-title function_">splice</span>(j+<span class="hljs-number">1</span>,<span class="hljs-number">0</span>,_arr.<span class="hljs-title function_">splice</span>(i,<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>]);
        }
    }
    <span class="hljs-keyword">return</span> _arr;
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 对10个数进行排序
 * <span class="hljs-doctag">@params</span>: number[]
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">arr:number[]</span>){
    <span class="hljs-keyword">let</span> _arr=<span class="hljs-title function_">insertionSortAsc</span>(arr)
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${arr}</span> 排序后的顺序是<span class="hljs-subst">${_arr}</span>`</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">test</span>([<span class="hljs-number">1</span>,<span class="hljs-number">45</span>,<span class="hljs-number">564</span>,<span class="hljs-number">234</span>,<span class="hljs-number">45</span>,<span class="hljs-number">234</span>,<span class="hljs-number">2342</span>,<span class="hljs-number">765</span>,<span class="hljs-number">234</span>,<span class="hljs-number">213</span>])
<span class="hljs-comment">// 1,45,564,234,45,234,2342,765,234,213 排序后的顺序是1,45,45,213,234,234,234,564,765,2342</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>这里题目说的是对10个数排序，所以我们选择了插入排序法来完成</p>
<h3 id="问题：有一个已经排好序的数组。现输入一个数，要求按原来的规律将它插入数组中。"><a href="#问题：有一个已经排好序的数组。现输入一个数，要求按原来的规律将它插入数组中。">问题：有一个已经排好序的数组。现输入一个数，要求按原来的规律将它插入数组中。</a></h3><p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 对一个已经排好序的数组，现输入一个数，要求按原来的规律将它插入数组中
 * <span class="hljs-doctag">@params</span>: number[]
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: number[]
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">insertSort</span>(<span class="hljs-params">arr:number[],num</span>):number[] {
    <span class="hljs-keyword">let</span> _arr=arr.<span class="hljs-title function_">concat</span>()
    <span class="hljs-keyword">if</span>(_arr[_arr.<span class="hljs-property">length</span>-<span class="hljs-number">1</span>]&lt;_arr[<span class="hljs-number">0</span>]){
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;_arr.<span class="hljs-property">length</span>;i++){
            <span class="hljs-keyword">if</span>(_arr[i]&lt;num){
                _arr.<span class="hljs-title function_">splice</span>(i,<span class="hljs-number">0</span>,num)
                <span class="hljs-keyword">return</span> _arr
            }
        }
        _arr.<span class="hljs-title function_">unshift</span>(num)
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;_arr.<span class="hljs-property">length</span>;i++){
            <span class="hljs-keyword">if</span>(_arr[i]&gt;num){
                _arr.<span class="hljs-title function_">splice</span>(i,<span class="hljs-number">0</span>,num)
                <span class="hljs-keyword">return</span> _arr
            }
        }
        _arr.<span class="hljs-title function_">push</span>(num)
    }
    <span class="hljs-keyword">return</span> _arr
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 有一个已经排好序的数组。现输入一个数，要求按原来的规律将它插入数组中
 * <span class="hljs-doctag">@params</span>: number[]
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">arr:number[],num:number</span>){
    <span class="hljs-keyword">let</span> _arr=<span class="hljs-title function_">insertSort</span>(arr,num)
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${arr}</span> 插入后结果是：<span class="hljs-subst">${_arr}</span>`</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">test</span>([<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">3453</span>,<span class="hljs-number">453345</span>,<span class="hljs-number">4534533</span>],<span class="hljs-number">10</span>)
<span class="hljs-comment">// 1,3,3453,453345,4534533 插入后结果是：1,3,10,3453,453345,4534533</span>

<span class="hljs-title function_">test</span>([<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">3453</span>,<span class="hljs-number">453345</span>,<span class="hljs-number">4534533</span>].<span class="hljs-title function_">reverse</span>(),<span class="hljs-number">10</span>)
<span class="hljs-comment">// 4534533,453345,3453,3,1 插入后结果是：4534533,453345,3453,10,3,1</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>我们这里是遍历的方式来找出适合我们插入的数字的位置的，你还可以试下用二分法的思想来插入，如果数组长度非常大的话，二分法的效率可能会高很多</p>
<h3 id="问题：一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。"><a href="#问题：一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。">问题：一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。</a></h3><p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 判断一个数字是否是回文数
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: boolean
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">palindromenumber</span>(<span class="hljs-params">num:number=<span class="hljs-number">1</span></span>):boolean{
    <span class="hljs-keyword">if</span>(num&lt;<span class="hljs-number">1</span> &amp;&amp; <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>(num)!==num){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'请输入正整数'</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
    }
    <span class="hljs-keyword">let</span> tmp=num.<span class="hljs-title function_">toString</span>().<span class="hljs-title function_">split</span>(<span class="hljs-string">''</span>)
    <span class="hljs-keyword">let</span> len=tmp.<span class="hljs-property">length</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;len;i++){
        <span class="hljs-keyword">if</span>(i&gt;=len-<span class="hljs-number">1</span>-i){
            <span class="hljs-keyword">break</span>
        }
        <span class="hljs-keyword">if</span>(tmp[i]!==tmp[len-<span class="hljs-number">1</span>-i]){
            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">palindromenumber</span>(<span class="hljs-number">12321</span>))
<span class="hljs-comment">// true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">palindromenumber</span>(<span class="hljs-number">123</span>))
<span class="hljs-comment">// false</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：判断101-200之间有多少个素数，并输出所有素数。"><strong>问题</strong>：判断101-200之间有多少个素数，并输出所有素数。</h3><p>tips:</p>
<h5 id="什么是素数？">  什么是素数？</h5><p>  素数又称质数，一个大于1的自然数，除了1和它自身外，不能被其他自然数整除的数叫做质数；否则称为合数。</p>
<h5 id="如何判断一个数是不是素数？">  如何判断一个数是不是素数？</h5><p>  在一般领域，对正整数n，如果用2到这个数的平方根<br>之间的所有整数去除，均无法整除，则n为质数。<br>  质数大于等于2 不能被它本身和1以外的数整除</p>
<p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 判断一个自然数是否是质数
 * <span class="hljs-doctag">@params</span>: 自然数
 * <span class="hljs-doctag">@retrun</span>: boolean(true:是质数，false:不是质数)
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">isPrime</span>(<span class="hljs-params">num:number</span>):boolean{
    <span class="hljs-keyword">let</span> numSqrt=<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">sqrt</span>(num);
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>;i&lt;numSqrt+<span class="hljs-number">1</span>;i++){
        <span class="hljs-keyword">if</span>(num%i == <span class="hljs-number">0</span>){
            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 判断两个数字(包含n1，不包含n2)之间的质数有哪些，并返回这两个数字之间的所有质数组成的列表
 * <span class="hljs-doctag">@params</span>: (n1,n2) 都是大于1的自然数，n1:查找大于等于n1的质数，n2：查找小于n2的质数
 * <span class="hljs-doctag">@retrun</span>: 列表
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">rangeNumPrime</span>(<span class="hljs-params">n1:number,n2:number</span>):number[]{
    <span class="hljs-keyword">if</span>(n1&lt;<span class="hljs-number">2</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">"素数范围是大于1的自然数"</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">if</span>(n1&gt;=n2){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">"第二个参数必须大于第一个参数"</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">let</span> primeNums=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> num =n1;num&lt;n2;num++){
        <span class="hljs-keyword">if</span>(<span class="hljs-title function_">isPrime</span>(num)){
            primeNums.<span class="hljs-title function_">push</span>(num)
        }
    }
    <span class="hljs-keyword">return</span> primeNums
}

<span class="hljs-comment">// 以列表的形式打印出101(包含)到200(不包含)的质数</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">rangeNumPrime</span>(<span class="hljs-number">101</span>,<span class="hljs-number">200</span>))

<span class="hljs-comment">// 打印結果</span>
[
  <span class="hljs-number">101</span>, <span class="hljs-number">103</span>, <span class="hljs-number">107</span>, <span class="hljs-number">109</span>, <span class="hljs-number">113</span>,
  <span class="hljs-number">127</span>, <span class="hljs-number">131</span>, <span class="hljs-number">137</span>, <span class="hljs-number">139</span>, <span class="hljs-number">149</span>,
  <span class="hljs-number">151</span>, <span class="hljs-number">157</span>, <span class="hljs-number">163</span>, <span class="hljs-number">167</span>, <span class="hljs-number">173</span>,
  <span class="hljs-number">179</span>, <span class="hljs-number">181</span>, <span class="hljs-number">191</span>, <span class="hljs-number">193</span>, <span class="hljs-number">197</span>,
  <span class="hljs-number">199</span>
]</span>
</code><span class="copy-button">复制代码</span></pre>
<h4 id="问题：一球从100米高度自由落下，每次落地后反跳回原高度的一半；再落下，求它在 第10次落地时，共经过多少米？第10次反弹多高？"><a href="#问题：一球从100米高度自由落下，每次落地后反跳回原高度的一半；再落下，求它在 第10次落地时，共经过多少米？第10次反弹多高？">问题：一球从100米高度自由落下，每次落地后反跳回原高度的一半；再落下，求它在 第10次落地时，共经过多少米？第10次反弹多高？</a></h4><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 经过times次反弹后，算出经过了多少距离和当次高度
 * <span class="hljs-doctag">@params</span>: (number)初始高度
 * <span class="hljs-doctag">@params</span>: (number)反跳次数
 * <span class="hljs-doctag">@retrun</span>: object
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">moutionComputing</span>(<span class="hljs-params">initHeight:number=<span class="hljs-number">0</span>,times:number=<span class="hljs-number">0</span></span>):object{
    <span class="hljs-keyword">let</span> totalLength=<span class="hljs-number">0</span>
    <span class="hljs-keyword">let</span> reboundHeight=initHeight
    <span class="hljs-keyword">while</span>(reboundHeight&gt;<span class="hljs-number">0</span> &amp;&amp; times&gt;<span class="hljs-number">0</span>){
        --times
        totalLength +=reboundHeight*<span class="hljs-number">1.5</span>
        reboundHeight=reboundHeight/<span class="hljs-number">2</span>
    }
    <span class="hljs-keyword">return</span> {
        totalLength,
        reboundHeight
    }
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-keyword">let</span> tmp=<span class="hljs-title function_">moutionComputing</span>(<span class="hljs-number">100</span>,<span class="hljs-number">10</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`从100米高度自由落下，每次落地后反跳回原高度的一半；再落下，它在 第10次落地时，共经过<span class="hljs-subst">${tmp.totalLength}</span>米,第10次反弹<span class="hljs-subst">${tmp.reboundHeight}</span>米？`</span>)

<span class="hljs-comment">// 从100米高度自由落下，每次落地后反跳回原高度的一半；再落下，它在 第10次落地时，共经过299.70703125米,第10次反弹0.09765625米？</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：输入三个整数x,y,z，请把这三个数由小到大输出"><a href="#问题：输入三个整数x,y,z，请把这三个数由小到大输出">问题：输入三个整数x,y,z，请把这三个数由小到大输出</a></h3><p>  tips:最简单的办法就是直接排序就可以了，我们这里使用js自带的sort排序就可以了，代码比较简单，</p>
<p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 传入三个整数x,y,z，把这三个数由小到大输出
 * <span class="hljs-doctag">@params</span>: (number) x
 * <span class="hljs-doctag">@params</span>: (number) y
 * <span class="hljs-doctag">@params</span>: (number) z
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">printSortNum</span>(<span class="hljs-params">x:number,y:number,z:number</span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> arr=[x,y,z]
    arr.<span class="hljs-title function_">sort</span>(<span class="hljs-function">(<span class="hljs-params">prev,curr</span>)=&gt;</span>{
        <span class="hljs-keyword">return</span> prev - curr
    })
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr.<span class="hljs-title function_">toString</span>())
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">printSortNum</span>(<span class="hljs-number">2</span>,<span class="hljs-number">546</span>,<span class="hljs-number">23</span>)
<span class="hljs-comment">// 2,23,546</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：输出所有的水仙花数"><strong>问题</strong>：输出所有的"水仙花数"</h3><p>tips:</p>
<h5 id="什么是水仙花数？">  什么是水仙花数？</h5><p>  水仙花数（Narcissistic number）也被称为超完全数字不变数（pluperfect digital invariant, PPDI）、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数（Armstrong number），水仙花数是指一个 3 位数，它的每个位上的数字的 3次幂之和等于它本身（例如：1^3 + 5^3+ 3^3 = 153）</p>
<p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 判断一个自然数是否是阿姆斯特朗数
 * <span class="hljs-doctag">@params</span>: num 自然数
 * <span class="hljs-doctag">@retrun</span>: boolean(true:是阿姆斯特朗数，false:不是阿姆斯特朗数)
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">isNarcissisticNumber</span>(<span class="hljs-params">num:number</span>):boolean{
    <span class="hljs-keyword">let</span> <span class="hljs-attr">_arr</span>:any[]=num.<span class="hljs-title function_">toString</span>().<span class="hljs-title function_">split</span>(<span class="hljs-string">''</span>)
    <span class="hljs-keyword">let</span> sum=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">let</span> len=_arr.<span class="hljs-property">length</span>;
    _arr.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">item</span>=&gt;</span>{
        sum +=<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">pow</span>(item,len)
    })
    <span class="hljs-keyword">if</span>(sum===num){
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 打印区间内阿姆斯特朗数
 * <span class="hljs-doctag">@params</span>: n1 自然数
 * <span class="hljs-doctag">@params</span>: n2 自然数,需大于n1
 * <span class="hljs-doctag">@retrun</span>: []
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">printTestNarcissisticNumber</span>(<span class="hljs-params">n1:number,n2:number</span>){
    <span class="hljs-keyword">let</span> <span class="hljs-attr">tmp</span>:number[]=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i= n1;i&lt;n2;i++){
        <span class="hljs-keyword">if</span>(<span class="hljs-title function_">isNarcissisticNumber</span>(i)){
            tmp.<span class="hljs-title function_">push</span>(i)
        }
    }
    <span class="hljs-keyword">return</span> tmp
}

<span class="hljs-comment">// 运行打印结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`水仙花数： <span class="hljs-subst">${printTestNarcissisticNumber(<span class="hljs-number">100</span>,<span class="hljs-number">1000</span>).toString()}</span>`</span>)
水仙花数： <span class="hljs-number">153</span>,<span class="hljs-number">370</span>,<span class="hljs-number">371</span>,<span class="hljs-number">407</span>

<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`四叶玫瑰数： <span class="hljs-subst">${printTestNarcissisticNumber(<span class="hljs-number">1000</span>,<span class="hljs-number">10000</span>).toString()}</span>`</span>)
四叶玫瑰数： <span class="hljs-number">1634</span>,<span class="hljs-number">8208</span>,<span class="hljs-number">9474</span>

<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`五角星数： <span class="hljs-subst">${printTestNarcissisticNumber(<span class="hljs-number">10000</span>,<span class="hljs-number">100000</span>).toString()}</span>`</span>)
五角星数： <span class="hljs-number">54748</span>,<span class="hljs-number">92727</span>,<span class="hljs-number">93084</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：两个乒乓球队进行比赛，各出三人。甲队为a,b,c三人，乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比，c说他不和x,z比，请编程序找出三队赛手的名单。"><a href="#问题：两个乒乓球队进行比赛，各出三人。甲队为a,b,c三人，乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比，c说他不和x,z比，请编程序找出三队赛手的名单。">问题：两个乒乓球队进行比赛，各出三人。甲队为a,b,c三人，乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比，c说他不和x,z比，请编程序找出三队赛手的名单。</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 乒乓球队进行比赛名单问题
 * 两个乒乓球队进行比赛，各出三人。甲队为a,b,c三人，乙队为x,y,z三人。
 * 已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比，c说他不和x,z比，请编程序找出三队赛手的名单。
 * <span class="hljs-doctag">@params</span>: null
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">competitionList</span>(<span class="hljs-params"></span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> firstTeam={
        <span class="hljs-attr">a</span>:{
            <span class="hljs-attr">name</span>:<span class="hljs-string">'a'</span>,
            <span class="hljs-attr">impossibleOpponent</span>:[<span class="hljs-string">'x'</span>],
            <span class="hljs-attr">possibleRivals</span>:[],
            <span class="hljs-attr">rival</span>:<span class="hljs-string">''</span>
        },
        <span class="hljs-attr">b</span>:{
            <span class="hljs-attr">name</span>:<span class="hljs-string">'b'</span>,
            <span class="hljs-attr">impossibleOpponent</span>:[],
            <span class="hljs-attr">possibleRivals</span>:[],
            <span class="hljs-attr">rival</span>:<span class="hljs-string">''</span>
        },
        <span class="hljs-attr">c</span>:{
            <span class="hljs-attr">name</span>:<span class="hljs-string">'c'</span>,
            <span class="hljs-attr">impossibleOpponent</span>:[<span class="hljs-string">'x'</span>,<span class="hljs-string">'z'</span>],
            <span class="hljs-attr">possibleRivals</span>:[],
            <span class="hljs-attr">rival</span>:<span class="hljs-string">''</span>
        }
    }
    <span class="hljs-keyword">let</span> secondTeam=[<span class="hljs-string">'x'</span>,<span class="hljs-string">'y'</span>,<span class="hljs-string">'z'</span>]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;secondTeam.<span class="hljs-property">length</span>;i++){
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> p1 <span class="hljs-keyword">in</span> firstTeam){
            <span class="hljs-keyword">if</span>(firstTeam[p1].<span class="hljs-property">impossibleOpponent</span>.<span class="hljs-title function_">indexOf</span>(secondTeam[i])===-<span class="hljs-number">1</span> &amp;&amp; firstTeam[p1].<span class="hljs-property">possibleRivals</span>.<span class="hljs-title function_">indexOf</span>(secondTeam[i])===-<span class="hljs-number">1</span>){
                firstTeam[p1].<span class="hljs-property">possibleRivals</span>.<span class="hljs-title function_">push</span>(secondTeam[i])
            }
        }
    }
    <span class="hljs-keyword">let</span> hadRival=[]
    <span class="hljs-keyword">while</span>(<span class="hljs-literal">true</span>){
        <span class="hljs-keyword">let</span> meaningfulOperation=<span class="hljs-number">0</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> p1 <span class="hljs-keyword">in</span> firstTeam){
            <span class="hljs-keyword">if</span>(firstTeam[p1].<span class="hljs-property">possibleRivals</span>.<span class="hljs-property">length</span>===<span class="hljs-number">1</span>){
                firstTeam[p1].<span class="hljs-property">rival</span>=firstTeam[p1].<span class="hljs-property">possibleRivals</span>[<span class="hljs-number">0</span>]
                hadRival.<span class="hljs-title function_">push</span>(firstTeam[p1].<span class="hljs-property">rival</span>)
                ++meaningfulOperation
                firstTeam[p1].<span class="hljs-property">possibleRivals</span>.<span class="hljs-property">length</span>=<span class="hljs-number">0</span>
            }<span class="hljs-keyword">else</span>{
                <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> r =<span class="hljs-number">0</span>;r&lt;firstTeam[p1].<span class="hljs-property">possibleRivals</span>.<span class="hljs-property">length</span>;r++){
                    <span class="hljs-keyword">if</span>(hadRival.<span class="hljs-title function_">indexOf</span>(firstTeam[p1].<span class="hljs-property">possibleRivals</span>[r])&gt;-<span class="hljs-number">1</span>){
                        firstTeam[p1].<span class="hljs-property">possibleRivals</span>.<span class="hljs-title function_">splice</span>(r,<span class="hljs-number">1</span>)
                        --r
                        ++meaningfulOperation
                    }
                }
            }
        }
        <span class="hljs-keyword">if</span>(meaningfulOperation===<span class="hljs-number">0</span>){
            <span class="hljs-keyword">break</span>;
        }
    }
    <span class="hljs-keyword">let</span> printStr=<span class="hljs-string">''</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> p1 <span class="hljs-keyword">in</span> firstTeam){
        printStr += <span class="hljs-string">`<span class="hljs-subst">${printStr?<span class="hljs-string">','</span>:<span class="hljs-string">''</span>}</span><span class="hljs-subst">${firstTeam[p1].name}</span>的对手是<span class="hljs-subst">${firstTeam[p1].rival}</span>`</span>
    }
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(printStr)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">competitionList</span>()
<span class="hljs-comment">// a的对手是z,b的对手是x,c的对手是y</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：取一个整数a从右端开始的4～7位。"><a href="#问题：取一个整数a从右端开始的4～7位。">问题：取一个整数a从右端开始的4～7位。</a></h3><p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 取一个整数a从右端开始的4～7位
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">num:number</span>) {
    <span class="hljs-keyword">let</span> str=num.<span class="hljs-title function_">toString</span>()
    <span class="hljs-keyword">if</span>(str.<span class="hljs-property">length</span>&lt;<span class="hljs-number">7</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num}</span> 的长度小于7位`</span>)
        <span class="hljs-keyword">return</span>
    }
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num}</span> 的从右端开始的4～7位是：<span class="hljs-subst">${num.toString().substr(-<span class="hljs-number">7</span>,<span class="hljs-number">3</span>)}</span>`</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">test</span>(<span class="hljs-number">1234567</span>);
<span class="hljs-number">1234567</span> 的从右端开始的<span class="hljs-number">4</span>～<span class="hljs-number">7</span>位是：<span class="hljs-number">123</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：猴子吃桃问题：猴子第一天摘下若干个桃子，当即吃了一半，还不瘾，又多吃了一个 第二天早上又将剩下的桃子吃掉一半，又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时，见只剩下一个桃子了。求第一天共摘了多少。"><a href="#问题：猴子吃桃问题：猴子第一天摘下若干个桃子，当即吃了一半，还不瘾，又多吃了一个 第二天早上又将剩下的桃子吃掉一半，又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时，见只剩下一个桃子了。求第一天共摘了多少。">问题：猴子吃桃问题：猴子第一天摘下若干个桃子，当即吃了一半，还不瘾，又多吃了一个 第二天早上又将剩下的桃子吃掉一半，又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时，见只剩下一个桃子了。求第一天共摘了多少。</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 猴子吃桃问题
 * <span class="hljs-doctag">@params</span>: null
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">monkeyEatingPeach</span>(<span class="hljs-params"></span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> peachesNum=<span class="hljs-number">1</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-number">9</span>;i++){
        peachesNum = (peachesNum+<span class="hljs-number">1</span>)*<span class="hljs-number">2</span>
    }
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`第一天共摘了<span class="hljs-subst">${peachesNum}</span>桃子`</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">monkeyEatingPeach</span>()
<span class="hljs-comment">// 第一天共摘了1534桃子</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：将一个正整数分解质因数。例如：输入90,打印出90=233*5。">问题：将一个正整数分解质因数。例如：输入90,打印出90=2<em>3</em>3*5。</h3><p>tips:</p>
<h5 id="什么是正整数？">  什么是正整数？</h5><p>  和整数一样，正整数也是一个可数的无限集合。在数论中，正整数，即1、2、3……；但在集合论和计算机科学中，自然数则通常是指非负整数，即正整数与0的集合，也可以说成是除了0以外的自然数就是正整数。正整数又可分为质数，1和合数。正整数可带正号（+），也可以不带。</p>
<h5 id="什么是质因数？">  什么是质因数？</h5><p>  质因数（素因数或质因子）在数论里是指能整除给定正整数的质数。除了1以外，两个没有其他共同质因子的正整数称为互质。因为1没有质因子，1与任何正整数（包括1本身）都是互质。正整数的因数分解可将正整数表示为一连串的质因子相乘，质因子如重复可以用指数表示。根据算术基本定理，任何正整数皆有独一无二的质因子分解式 [1]  。只有一个质因子的正整数为质数。</p>
<p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 将一个正整数分解质因数
 * <span class="hljs-doctag">@params</span>: (number) 自然数
 * <span class="hljs-doctag">@retrun</span>: (Array) 质因数组成的数组
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">primeFactor</span>(<span class="hljs-params">num:number</span>):number[]{
    <span class="hljs-keyword">if</span>(num&lt;<span class="hljs-number">1</span> || <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">round</span>(num)!=num){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'参数必须是一个正整数'</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">primes</span>:number[]=[]
    <span class="hljs-keyword">let</span> <span class="hljs-attr">tmp</span>:number=num
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">2</span>;i&lt;num;i++){
        <span class="hljs-keyword">if</span>(i&gt;=tmp){
            primes.<span class="hljs-title function_">push</span>(tmp)
            <span class="hljs-keyword">break</span>;
        }
        <span class="hljs-keyword">while</span>(tmp%i===<span class="hljs-number">0</span>){
            primes.<span class="hljs-title function_">push</span>(i)
            tmp=tmp/i
        }
        <span class="hljs-keyword">if</span>(tmp&lt;<span class="hljs-number">2</span>){
            <span class="hljs-keyword">break</span>;
        }
    }
    <span class="hljs-keyword">return</span> primes
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 打印一个数的分解质因数
 * <span class="hljs-doctag">@params</span>: (number) 
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">printPrimes</span>(<span class="hljs-params">num:number</span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> primes=<span class="hljs-title function_">primeFactor</span>(num)
    <span class="hljs-keyword">if</span>(primes.<span class="hljs-property">length</span>&gt;<span class="hljs-number">0</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num}</span>的分解质因数结果：<span class="hljs-subst">${num}</span>=<span class="hljs-subst">${primes.join(<span class="hljs-string">'*'</span>)}</span>`</span>)
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num}</span>是一个质数`</span>)
    }
}

<span class="hljs-comment">//运行结果</span>
<span class="hljs-title function_">printPrimes</span>(<span class="hljs-number">90</span>)
<span class="hljs-number">90</span>的分解质因数结果：<span class="hljs-number">90</span>=<span class="hljs-number">2</span>*<span class="hljs-number">3</span>*<span class="hljs-number">3</span>*<span class="hljs-number">5</span>

<span class="hljs-title function_">printPrimes</span>(<span class="hljs-number">900</span>)
<span class="hljs-number">900</span>的分解质因数结果：<span class="hljs-number">900</span>=<span class="hljs-number">2</span>*<span class="hljs-number">2</span>*<span class="hljs-number">3</span>*<span class="hljs-number">3</span>*<span class="hljs-number">5</span>*<span class="hljs-number">5</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：输入两个正整数m和n，求其最大公约数和最小公倍数。"><a href="#问题：输入两个正整数m和n，求其最大公约数和最小公倍数。">问题：输入两个正整数m和n，求其最大公约数和最小公倍数。</a></h3><p>tips:</p>
<h5 id="倍数和约数">  倍数和约数</h5><p>  如果数a能被数b整除，a就叫做b的倍数，b就叫做a的约数。约数和倍数都表示一个整数与另一个整数的关系，不能单独存在。</p>
<h5 id="什么是最大公约数？">  什么是最大公约数？</h5><p>  几个整数中公有的约数，叫做这几个数的公约数；其中最大的一个，叫做这几个数的最大公约数。例如：12、16的公约数有1、2、4，其中最大的一个是4，4是12与16的最大公约数，一般记为（12，16）=4。12、15、18的最大公约数是3，记为（12，15，18）=3。</p>
<h5 id="什么是最小公倍数？">  什么是最小公倍数？</h5><p>  几个自然数公有的倍数，叫做这几个数的公倍数，其中最小的一个自然数，叫做这几个数的最小公倍数。例如：4的倍数有4、8、12、16，……，6的倍数有6、12、18、24，……，4和6的公倍数有12、24，……，其中最小的是12，一般记为[4，6]=12。12、15、18的最小公倍数是180。记为[12，15，18]=180。若干个互质数的最小公倍数为它们的乘积的绝对值。</p>
<h5 id="求最大公约数和最小公倍数有什么方法？">  求最大公约数和最小公倍数有什么方法？</h5><p>1、质因数分解法<br>2、短除法<br>3、辗转相除法<br>4、更相减损法</p>
<p>  <strong>质因数分解法</strong> 的实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 将一个正整数分解质因数
 * <span class="hljs-doctag">@params</span>: (number) 自然数
 * <span class="hljs-doctag">@retrun</span>: (Array) 质因数组成的数组
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">primeFactor</span>(<span class="hljs-params">num:number</span>):number[]{
    <span class="hljs-keyword">if</span>(num&lt;<span class="hljs-number">1</span> || <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">round</span>(num)!=num){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'参数必须是一个正整数'</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">primes</span>:number[]=[<span class="hljs-number">1</span>]
    <span class="hljs-keyword">let</span> <span class="hljs-attr">tmp</span>:number=num
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">2</span>;i&lt;=num;i++){
        <span class="hljs-keyword">if</span>(i&gt;=tmp){
            primes.<span class="hljs-title function_">push</span>(tmp)
            <span class="hljs-keyword">break</span>;
        }
        <span class="hljs-keyword">while</span>(tmp%i===<span class="hljs-number">0</span>){
            primes.<span class="hljs-title function_">push</span>(i)
            tmp=tmp/i
        }
        <span class="hljs-keyword">if</span>(tmp&lt;<span class="hljs-number">2</span>){
            <span class="hljs-keyword">break</span>;
        }
    }
    <span class="hljs-keyword">return</span> primes
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 获取两个数的最小公倍数和最小公约数
 * <span class="hljs-doctag">@params</span>: (number) 
 * <span class="hljs-doctag">@params</span>: (number) 
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">printPrimes</span>(<span class="hljs-params">num1:number,num2:number</span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> primes1=<span class="hljs-title function_">primeFactor</span>(num1)
    <span class="hljs-keyword">let</span> primes2=<span class="hljs-title function_">primeFactor</span>(num2)
    <span class="hljs-keyword">let</span> commonPrimes=[]
    <span class="hljs-keyword">let</span> diffPrimes=[]
    <span class="hljs-keyword">while</span>(primes1.<span class="hljs-property">length</span>&gt;<span class="hljs-number">0</span>){
        <span class="hljs-keyword">let</span> tmp=primes1.<span class="hljs-title function_">shift</span>()
        <span class="hljs-keyword">if</span>(primes2.<span class="hljs-property">length</span>==<span class="hljs-number">0</span>){
            diffPrimes.<span class="hljs-title function_">push</span>(tmp)
        }<span class="hljs-keyword">else</span>{
            <span class="hljs-keyword">let</span> ind2=primes2.<span class="hljs-title function_">indexOf</span>(tmp)
            <span class="hljs-keyword">if</span>(ind2&gt;-<span class="hljs-number">1</span>){
                commonPrimes.<span class="hljs-title function_">push</span>(primes2.<span class="hljs-title function_">splice</span>(ind2,<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>])
            }<span class="hljs-keyword">else</span>{
                diffPrimes.<span class="hljs-title function_">push</span>(tmp)
            }
        }
    }
    <span class="hljs-keyword">if</span>(primes2.<span class="hljs-property">length</span>&gt;<span class="hljs-number">0</span>){
        diffPrimes = diffPrimes.<span class="hljs-title function_">concat</span>(primes2)
    }
    <span class="hljs-keyword">let</span> maxCommonDivisor=commonPrimes.<span class="hljs-title function_">reduce</span>(<span class="hljs-function">(<span class="hljs-params">total,val</span>)=&gt;</span>{
        <span class="hljs-keyword">return</span> total*val
    })
    <span class="hljs-keyword">let</span> <span class="hljs-title class_">LeastCommonMultiple</span>=commonPrimes.<span class="hljs-title function_">concat</span>(diffPrimes).<span class="hljs-title function_">reduce</span>(<span class="hljs-function">(<span class="hljs-params">total,val</span>)=&gt;</span>{
        <span class="hljs-keyword">return</span> total*val
    })
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num1}</span>和<span class="hljs-subst">${num2}</span>的最大公约数是：(<span class="hljs-subst">${num1}</span>,<span class="hljs-subst">${num2}</span>)=<span class="hljs-subst">${maxCommonDivisor}</span>`</span>)
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num1}</span>和<span class="hljs-subst">${num2}</span>的最小公倍数是：[<span class="hljs-subst">${num1}</span>,<span class="hljs-subst">${num2}</span>]=<span class="hljs-subst">${LeastCommonMultiple}</span>`</span>)
}

<span class="hljs-title function_">printPrimes</span>(<span class="hljs-number">12</span>,<span class="hljs-number">16</span>)

<span class="hljs-comment">// 运行测试</span>
<span class="hljs-title function_">printPrimes</span>(<span class="hljs-number">12</span>,<span class="hljs-number">16</span>)
<span class="hljs-number">12</span>和<span class="hljs-number">16</span>的最大公约数是：(<span class="hljs-number">12</span>,<span class="hljs-number">16</span>)=<span class="hljs-number">4</span>
<span class="hljs-number">12</span>和<span class="hljs-number">16</span>的最小公倍数是：[<span class="hljs-number">12</span>,<span class="hljs-number">16</span>]=<span class="hljs-number">48</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>  <strong>辗转相除法</strong> 的实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 根据辗转相除法计算两个数的最大公约数
 * <span class="hljs-doctag">@params</span>: (number) num1
 * <span class="hljs-doctag">@params</span>: (number) num2
 * <span class="hljs-doctag">@retrun</span>: (number) 最大公约数
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">greatestCommonDivisor</span>(<span class="hljs-params">num1:number,num2:number</span>):number{
    <span class="hljs-keyword">let</span> <span class="hljs-attr">remainder</span>:number=num2
    <span class="hljs-keyword">while</span>(remainder!==<span class="hljs-number">0</span>){
        num2=remainder
        remainder=num1%num2
        num1=num2
    }
    <span class="hljs-keyword">return</span> num2
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 计算两个数的最小公倍数
 * <span class="hljs-doctag">@params</span>: (number) num1
 * <span class="hljs-doctag">@params</span>: (number) num2
 * <span class="hljs-doctag">@retrun</span>: (number) 最小公倍数
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">leastCommonMultiple</span>(<span class="hljs-params">num1:number,num2:number</span>):number{
    <span class="hljs-keyword">let</span> <span class="hljs-attr">remainder</span>:number=<span class="hljs-title function_">greatestCommonDivisor</span>(num1,num2)
    <span class="hljs-keyword">return</span> num1*num2/remainder
}

<span class="hljs-keyword">function</span> <span class="hljs-title function_">printTest</span>(<span class="hljs-params">num1:number,num2:number</span>){
    <span class="hljs-keyword">let</span> maxCommonDivisor=<span class="hljs-title function_">greatestCommonDivisor</span>(num1,num2)
    <span class="hljs-keyword">let</span> _leastCommon=<span class="hljs-title function_">leastCommonMultiple</span>(num1,num2,maxCommonDivisor)
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num1}</span>和<span class="hljs-subst">${num2}</span>的最大公约数是：(<span class="hljs-subst">${num1}</span>,<span class="hljs-subst">${num2}</span>)=<span class="hljs-subst">${maxCommonDivisor}</span>`</span>)
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num1}</span>和<span class="hljs-subst">${num2}</span>的最小公倍数是：[<span class="hljs-subst">${num1}</span>,<span class="hljs-subst">${num2}</span>]=<span class="hljs-subst">${_leastCommon}</span>`</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">printTest</span>(<span class="hljs-number">120</span>, <span class="hljs-number">24</span>);
<span class="hljs-number">120</span>和<span class="hljs-number">24</span>的最大公约数是：(<span class="hljs-number">120</span>,<span class="hljs-number">24</span>)=<span class="hljs-number">24</span>
<span class="hljs-number">120</span>和<span class="hljs-number">24</span>的最小公倍数是：[<span class="hljs-number">120</span>,<span class="hljs-number">24</span>]=<span class="hljs-number">120</span>

<span class="hljs-title function_">printTest</span>(<span class="hljs-number">10</span>, <span class="hljs-number">23</span>);
<span class="hljs-number">10</span>和<span class="hljs-number">23</span>的最大公约数是：(<span class="hljs-number">10</span>,<span class="hljs-number">23</span>)=<span class="hljs-number">1</span>
<span class="hljs-number">10</span>和<span class="hljs-number">23</span>的最小公倍数是：[<span class="hljs-number">10</span>,<span class="hljs-number">23</span>]=<span class="hljs-number">230</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：有1、2、3、4个数字，能组成多少个互不相同且无重复数字的三位数？都是多少？"><a href="#问题：有1、2、3、4个数字，能组成多少个互不相同且无重复数字的三位数？都是多少？">问题：有1、2、3、4个数字，能组成多少个互不相同且无重复数字的三位数？都是多少？</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 有1、2、3、4个数字，返回由互不相同且无重复数字的三位数组成的数组
 * <span class="hljs-doctag">@retrun</span>: array
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">diffNumber</span>(<span class="hljs-params"></span>):number[]{
    <span class="hljs-keyword">let</span> <span class="hljs-attr">tmp</span>:number[]=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">1</span>;i&lt;<span class="hljs-number">5</span>;i++){
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> j=<span class="hljs-number">1</span>;j&lt;<span class="hljs-number">5</span>;j++){
            <span class="hljs-keyword">if</span>(i==j){
                <span class="hljs-keyword">continue</span>
            }
            <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> k=<span class="hljs-number">1</span>;k&lt;<span class="hljs-number">5</span>;k++){
                <span class="hljs-keyword">if</span>(i==k || j==k){
                    <span class="hljs-keyword">continue</span>
                }
                tmp.<span class="hljs-title function_">push</span>(i*<span class="hljs-number">100</span>+j*<span class="hljs-number">10</span>+k)
            }
        }
    }
    <span class="hljs-keyword">return</span> tmp
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-keyword">let</span> tmpDiffNum=<span class="hljs-title function_">diffNumber</span>()
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`有1、2、3、4个数字，能组成<span class="hljs-subst">${tmpDiffNum.length}</span>个互不相同且无重复数字的三位数,是：<span class="hljs-subst">${tmpDiffNum.toString()}</span>`</span>)

<span class="hljs-comment">// 有1、2、3、4个数字，能组成24个互不相同且无重复数字的三位数,是：123,124,132,134,142,143,213,214,231,234,241,243,312,314,321,324,341,342,412,413,421,423,431,432</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：分别统计出一个字符串中英文字母、空格、数字和其它字符的个数。"><a href="#问题：分别统计出一个字符串中英文字母、空格、数字和其它字符的个数。">问题：分别统计出一个字符串中英文字母、空格、数字和其它字符的个数。</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 分别统计出一个字符串中英文字母、空格、数字和其它字符的个数
 * <span class="hljs-doctag">@params</span>: (string) str
 * <span class="hljs-doctag">@retrun</span>: (map)
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">greatestCommonDivisor</span>(<span class="hljs-params">str:string</span>){
    <span class="hljs-keyword">let</span> map=<span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>&lt;any, number&gt;()
    str.<span class="hljs-title function_">split</span>(<span class="hljs-string">''</span>).<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">item</span>=&gt;</span>{
        map.<span class="hljs-title function_">set</span>(item,(map.<span class="hljs-title function_">get</span>(item) || <span class="hljs-number">0</span>)+<span class="hljs-number">1</span>)
    })
    map.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">val,key</span>)=&gt;</span>{
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`字符 <span class="hljs-subst">${key}</span> 有 <span class="hljs-subst">${val}</span> 个`</span>)
    })
}


<span class="hljs-comment">// 运行结果</span>
<span class="hljs-title function_">greatestCommonDivisor</span>(<span class="hljs-string">`sfasdf4234@##@!$%$&amp;&amp;**&amp;(*&amp;~&lt;&gt;?:"|   
dfsd
dfasft
dfqwr23
  `</span>)
字符 s 有 <span class="hljs-number">4</span> 个
字符 f 有 <span class="hljs-number">6</span> 个
字符 a 有 <span class="hljs-number">2</span> 个
字符 d 有 <span class="hljs-number">5</span> 个
字符 <span class="hljs-number">4</span> 有 <span class="hljs-number">2</span> 个
字符 <span class="hljs-number">2</span> 有 <span class="hljs-number">2</span> 个
字符 <span class="hljs-number">3</span> 有 <span class="hljs-number">2</span> 个
字符 @ 有 <span class="hljs-number">2</span> 个
字符 # 有 <span class="hljs-number">2</span> 个
字符 ! 有 <span class="hljs-number">1</span> 个
字符 $ 有 <span class="hljs-number">2</span> 个
字符 % 有 <span class="hljs-number">1</span> 个
字符 &amp; 有 <span class="hljs-number">4</span> 个
字符 * 有 <span class="hljs-number">3</span> 个
字符 ( 有 <span class="hljs-number">1</span> 个
字符 ~ 有 <span class="hljs-number">1</span> 个
字符 &lt; 有 <span class="hljs-number">1</span> 个
字符 &gt; 有 <span class="hljs-number">1</span> 个
字符 ? 有 <span class="hljs-number">1</span> 个
字符 : 有 <span class="hljs-number">1</span> 个
字符 <span class="hljs-string">" 有 1 个
字符 | 有 1 个
字符   有 5 个
字符 
 有 4 个
字符 t 有 1 个
字符 q 有 1 个
字符 w 有 1 个
字符 r 有 1 个</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：求s=a+aa+aaa+aaaa+aa…a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)"><a href="#问题：求s=a+aa+aaa+aaaa+aa…a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)">问题：求s=a+aa+aaa+aaaa+aa…a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 传入一个数字，计算出这个数字从一位到多为重复组成的数字的和
 * <span class="hljs-doctag">@params</span>: (number) num
 * <span class="hljs-doctag">@retrun</span>: (number)
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">getMajoritySum</span>(<span class="hljs-params">num:number,totalSum:number=<span class="hljs-number">5</span></span>):number {
    <span class="hljs-keyword">let</span> ind=<span class="hljs-number">0</span>
    <span class="hljs-keyword">let</span> sum=<span class="hljs-number">0</span>
    <span class="hljs-keyword">while</span>(totalSum&gt;<span class="hljs-number">0</span>){
        sum+=num*totalSum*<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">pow</span>(<span class="hljs-number">10</span>,ind)
        ++ind
        --totalSum
    }
    <span class="hljs-keyword">return</span> sum
}

<span class="hljs-comment">// 测试运行</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMajoritySum</span>(<span class="hljs-number">2</span>))
<span class="hljs-number">24690</span>

<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMajoritySum</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>))
<span class="hljs-number">1</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>  从上面的代码的实现思路其实是这样的：<br>如果要计算(2+22+222+2222+22222) 这个式子的和，那么我们看成是如下的式子的和:<br>(2<em>5+2</em>4<em>10+2</em>3<em>100+2</em>2<em>1000+2</em>1*10000)</p>
<h3 id="问题：有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13…求出这个数列的前20项之和"><a href="#问题：有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13…求出这个数列的前20项之和">问题：有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13…求出这个数列的前20项之和</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 分数序列求和
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: number
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">sequencesSummation</span>(<span class="hljs-params">itemlen:number=<span class="hljs-number">20</span></span>):number{
    <span class="hljs-keyword">if</span>(itemlen&lt;<span class="hljs-number">1</span> &amp;&amp; <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>(itemlen)!==itemlen){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'获取前多少项之和应为正整数'</span>)
        <span class="hljs-keyword">return</span>
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">sum</span>:number=<span class="hljs-number">0</span>
    <span class="hljs-keyword">let</span> currDenominator=<span class="hljs-number">2</span>
    <span class="hljs-keyword">let</span> currMolecule=<span class="hljs-number">1</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">1</span>;i&lt;=itemlen;i++){
        sum+= currDenominator/currMolecule
        <span class="hljs-keyword">let</span> tmp=currMolecule
        currMolecule=currDenominator
        currDenominator += tmp
    }
    <span class="hljs-keyword">return</span> sum
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`前20项之和是：<span class="hljs-subst">${sequencesSummation(<span class="hljs-number">20</span>)}</span>`</span>)
<span class="hljs-comment">// 前20项之和是：32.66026079864164</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：求1+2!+3!+…+20!的和"><a href="#问题：求1+2!+3!+…+20!的和">问题：求1+2!+3!+…+20!的和</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 累乘
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: number
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">multiply</span>(<span class="hljs-params">itemlen:number=<span class="hljs-number">20</span></span>):number{
    <span class="hljs-keyword">if</span>(itemlen&lt;<span class="hljs-number">1</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'计算累乘的项数须为正整数'</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
    }
    <span class="hljs-keyword">if</span>(itemlen&lt;<span class="hljs-number">1</span>){
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">sum</span>:number=<span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">1</span>;i&lt;=itemlen;i++){
        <span class="hljs-keyword">let</span> tmp=<span class="hljs-number">1</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> j=<span class="hljs-number">1</span>;j&lt;=i;j++){
            tmp *=j
        }
        sum += tmp;
    }
    <span class="hljs-keyword">return</span> sum
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`前20项之和是：<span class="hljs-subst">${multiply(<span class="hljs-number">20</span>)}</span>`</span>)
<span class="hljs-comment">// 前20项之和是：2561327494111820300</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：企业发放的奖金根据利润提成。利润(I)低于或等于10万元时，奖金可提10%；利润高于10万元，低于20万元时，低于10万元的部分按10%提成，高于10万元的部分，可可提成7.5%；20万到40万之间时，高于20万元的部分，可提成5%；40万到60万之间时高于40万元的部分，可提成3%；60万到100万之间时，高于60万元的部分，可提成1.5%，高于100万元时，超过100万元的部分按1%提成，求应发放奖金总数？"><a href="#问题：企业发放的奖金根据利润提成。利润(I)低于或等于10万元时，奖金可提10%；利润高于10万元，低于20万元时，低于10万元的部分按10%提成，高于10万元的部分，可可提成7.5%；20万到40万之间时，高于20万元的部分，可提成5%；40万到60万之间时高于40万元的部分，可提成3%；60万到100万之间时，高于60万元的部分，可提成1.5%，高于100万元时，超过100万元的部分按1%提成，求应发放奖金总数？">问题：企业发放的奖金根据利润提成。利润(I)低于或等于10万元时，奖金可提10%；利润高于10万元，低于20万元时，低于10万元的部分按10%提成，高于10万元的部分，可可提成7.5%；20万到40万之间时，高于20万元的部分，可提成5%；40万到60万之间时高于40万元的部分，可提成3%；60万到100万之间时，高于60万元的部分，可提成1.5%，高于100万元时，超过100万元的部分按1%提成，求应发放奖金总数？</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 企业发放的奖金问题，返回应发放奖金总数
 * <span class="hljs-doctag">@retrun</span>: number
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">getBounus</span>(<span class="hljs-params">profit:number=<span class="hljs-number">0</span></span>):number{
    <span class="hljs-keyword">let</span> sumBounus=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span>(profit&gt;<span class="hljs-number">1000000</span>){
        sumBounus +=(profit-<span class="hljs-number">1000000</span>)*<span class="hljs-number">0.01</span>
        profit = <span class="hljs-number">1000000</span>
    }
    <span class="hljs-keyword">if</span>(profit&gt;<span class="hljs-number">600000</span>){
        sumBounus +=(profit-<span class="hljs-number">600000</span>)*<span class="hljs-number">0.015</span>
        profit=<span class="hljs-number">600000</span>
    }
    <span class="hljs-keyword">if</span>(profit&gt;<span class="hljs-number">400000</span>){
        sumBounus +=(profit-<span class="hljs-number">400000</span>)*<span class="hljs-number">0.03</span>
        profit=<span class="hljs-number">400000</span>
    }
    <span class="hljs-keyword">if</span>(profit&gt;<span class="hljs-number">200000</span>){
        sumBounus +=(profit-<span class="hljs-number">200000</span>)*<span class="hljs-number">0.05</span>
        profit=<span class="hljs-number">200000</span>
    }
    <span class="hljs-keyword">if</span>(profit&gt;<span class="hljs-number">100000</span>){
        sumBounus +=(profit-<span class="hljs-number">100000</span>)*<span class="hljs-number">0.075</span>
        profit=<span class="hljs-number">100000</span>
    }
    <span class="hljs-keyword">if</span>(profit&lt;=<span class="hljs-number">100000</span>){
        sumBounus +=profit*<span class="hljs-number">0.1</span>
    }
    <span class="hljs-keyword">return</span> sumBounus
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-keyword">let</span> tmpBounus=<span class="hljs-title function_">getBounus</span>(<span class="hljs-number">2200000</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`当月利润2200000元时，求应发放奖金总数为：<span class="hljs-subst">${tmpBounus}</span>`</span>)
<span class="hljs-comment">// 当月利润2200000元时，求应发放奖金总数为：51500</span>

<span class="hljs-keyword">let</span> tmpBounus2=<span class="hljs-title function_">getBounus</span>(<span class="hljs-number">200000</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`当月利润200000元时，求应发放奖金总数为：<span class="hljs-subst">${tmpBounus2}</span>`</span>)
<span class="hljs-comment">// 当月利润200000元时，求应发放奖金总数为：17500</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：学习成绩=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。">问题：学习成绩&gt;=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。</h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 获取一个分数对应的等级
 * <span class="hljs-doctag">@params</span>: (number) 自然数
 * <span class="hljs-doctag">@retrun</span>: (string) 等级
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">getScoreGrade</span>(<span class="hljs-params">num:number</span>):string{
    <span class="hljs-keyword">if</span>(num&gt;=<span class="hljs-number">90</span>){
        <span class="hljs-keyword">return</span> <span class="hljs-string">'A'</span>
    }
    <span class="hljs-keyword">if</span>(num&gt;=<span class="hljs-number">60</span>){
        <span class="hljs-keyword">return</span> <span class="hljs-string">'B'</span>
    }
    <span class="hljs-keyword">return</span> <span class="hljs-string">'C'</span>
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 打印一个分数的等级
 * <span class="hljs-doctag">@params</span>: (number) 
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">printScoreGrade</span>(<span class="hljs-params">num:number</span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> grade=<span class="hljs-title function_">getScoreGrade</span>(num)
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`您的分数等级是<span class="hljs-subst">${grade}</span>`</span>)
}

<span class="hljs-comment">// 运行结果</span>
<span class="hljs-title function_">printScoreGrade</span>(<span class="hljs-number">90</span>)
您的分数等级是A</span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：利用递归方法求5!"><a href="#问题：利用递归方法求5!">问题：利用递归方法求5!</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 利用递归方法求阶乘
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: number
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">factorial</span>(<span class="hljs-params">num:number=<span class="hljs-number">5</span></span>):number{
    <span class="hljs-keyword">if</span>(num===<span class="hljs-number">1</span>){
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
    }
    <span class="hljs-keyword">return</span> num*<span class="hljs-title function_">factorial</span>(num-<span class="hljs-number">1</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`5的阶乘是：<span class="hljs-subst">${factorial(<span class="hljs-number">5</span>)}</span>`</span>)
<span class="hljs-number">5</span>的阶乘是：<span class="hljs-number">120</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>  在js里面，因为递归的方式是没有被底层引擎优化的，当需要递归的层数多了后会占用较多的内存，影响性能，我们下面再尝试使用循环的方式来实现阶乘：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 利用循环的方式求阶乘
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: number
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">factorial</span>(<span class="hljs-params">num:number=<span class="hljs-number">5</span></span>):number{
    <span class="hljs-keyword">let</span> <span class="hljs-attr">proNum</span>:number=<span class="hljs-number">1</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">1</span>;i&lt;=num;i++){
        proNum *= i
    }
    <span class="hljs-keyword">return</span> proNum
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`5的阶乘是：<span class="hljs-subst">${factorial(<span class="hljs-number">5</span>)}</span>`</span>)
<span class="hljs-number">5</span>的阶乘是：<span class="hljs-number">120</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：打印出杨辉三角形（要求打印出10行如下图）"><a href="#问题：打印出杨辉三角形（要求打印出10行如下图）">问题：打印出杨辉三角形（要求打印出10行如下图）</a></h3><p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 计算杨辉三角形各行数字
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: Array&lt;Array&lt;number&gt;&gt;
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">triangleYanghui</span>(<span class="hljs-params">num:number</span>):<span class="hljs-title class_">Array</span>&lt;<span class="hljs-title class_">Array</span>&lt;number&gt;&gt; {
    <span class="hljs-keyword">if</span>(num&lt;<span class="hljs-number">1</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`杨辉三角形行数不应低于1`</span>)
        <span class="hljs-keyword">return</span> [[]]
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">triangle</span>:<span class="hljs-title class_">Array</span>&lt;<span class="hljs-title class_">Array</span>&lt;number&gt;&gt;=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;num;i++){
        <span class="hljs-keyword">let</span> <span class="hljs-attr">tmp</span>:number[]=[]
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> j=<span class="hljs-number">1</span>;j&lt;=i+<span class="hljs-number">1</span>;j++){
            <span class="hljs-keyword">if</span>(j===<span class="hljs-number">1</span> || j===i+<span class="hljs-number">1</span>){
                tmp.<span class="hljs-title function_">push</span>(<span class="hljs-number">1</span>)
            }<span class="hljs-keyword">else</span>{
                tmp.<span class="hljs-title function_">push</span>(triangle[i-<span class="hljs-number">1</span>][j-<span class="hljs-number">2</span>]+triangle[i-<span class="hljs-number">1</span>][j-<span class="hljs-number">1</span>])
            }
        }
        triangle.<span class="hljs-title function_">push</span>(tmp)
    }
    <span class="hljs-keyword">return</span> triangle
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 打印出杨辉三角形
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">num:number,sizeSeat:number=<span class="hljs-number">5</span></span>):<span class="hljs-keyword">void</span> {
    <span class="hljs-keyword">let</span> _arr=<span class="hljs-title function_">triangleYanghui</span>(num)
    <span class="hljs-keyword">let</span> printStr=<span class="hljs-string">''</span>
    <span class="hljs-keyword">let</span> maxCol=_arr[_arr.<span class="hljs-property">length</span>-<span class="hljs-number">1</span>].<span class="hljs-property">length</span>
    <span class="hljs-keyword">let</span> space=<span class="hljs-string">' '</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;_arr.<span class="hljs-property">length</span>;i++){
        printStr += space.<span class="hljs-title function_">repeat</span>(sizeSeat * ((maxCol / <span class="hljs-number">2</span>) - (_arr[i].<span class="hljs-property">length</span> / <span class="hljs-number">2</span>)));
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> j=<span class="hljs-number">0</span>;j&lt;_arr[i].<span class="hljs-property">length</span>;j++){
            <span class="hljs-keyword">let</span> str=_arr[i][j].<span class="hljs-title function_">toString</span>()
            printStr += (space.<span class="hljs-title function_">repeat</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>((sizeSeat - str.<span class="hljs-property">length</span>) / <span class="hljs-number">2</span>)) + str+space.<span class="hljs-title function_">repeat</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">ceil</span>((sizeSeat - str.<span class="hljs-property">length</span>) / <span class="hljs-number">2</span>)));
        }
        <span class="hljs-keyword">if</span>(i&lt;_arr.<span class="hljs-property">length</span>-<span class="hljs-number">1</span>){
            printStr+=<span class="hljs-string">'\n'</span>
        }
    }
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(printStr)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">test</span>(<span class="hljs-number">10</span>)
                        <span class="hljs-number">1</span>  
                      <span class="hljs-number">1</span>    <span class="hljs-number">1</span>  
                   <span class="hljs-number">1</span>    <span class="hljs-number">2</span>    <span class="hljs-number">1</span>  
                 <span class="hljs-number">1</span>    <span class="hljs-number">3</span>    <span class="hljs-number">3</span>    <span class="hljs-number">1</span>  
              <span class="hljs-number">1</span>    <span class="hljs-number">4</span>    <span class="hljs-number">6</span>    <span class="hljs-number">4</span>    <span class="hljs-number">1</span>  
            <span class="hljs-number">1</span>    <span class="hljs-number">5</span>   <span class="hljs-number">10</span>   <span class="hljs-number">10</span>    <span class="hljs-number">5</span>    <span class="hljs-number">1</span>  
         <span class="hljs-number">1</span>    <span class="hljs-number">6</span>   <span class="hljs-number">15</span>   <span class="hljs-number">20</span>   <span class="hljs-number">15</span>    <span class="hljs-number">6</span>    <span class="hljs-number">1</span>  
       <span class="hljs-number">1</span>    <span class="hljs-number">7</span>   <span class="hljs-number">21</span>   <span class="hljs-number">35</span>   <span class="hljs-number">35</span>   <span class="hljs-number">21</span>    <span class="hljs-number">7</span>    <span class="hljs-number">1</span>  
    <span class="hljs-number">1</span>    <span class="hljs-number">8</span>   <span class="hljs-number">28</span>   <span class="hljs-number">56</span>   <span class="hljs-number">70</span>   <span class="hljs-number">56</span>   <span class="hljs-number">28</span>    <span class="hljs-number">8</span>    <span class="hljs-number">1</span>  
  <span class="hljs-number">1</span>    <span class="hljs-number">9</span>   <span class="hljs-number">36</span>   <span class="hljs-number">84</span>   <span class="hljs-number">126</span>  <span class="hljs-number">126</span>  <span class="hljs-number">84</span>   <span class="hljs-number">36</span>    <span class="hljs-number">9</span>    <span class="hljs-number">1</span>  </span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：请输入星期几的第一个字母来判断一下是星期几，如果第一个字母一样，则继续判断第二个字母。"><a href="#问题：请输入星期几的第一个字母来判断一下是星期几，如果第一个字母一样，则继续判断第二个字母。">问题：请输入星期几的第一个字母来判断一下是星期几，如果第一个字母一样，则继续判断第二个字母。</a></h3><p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 第一个字母来判断一下是星期几，如果第一个字母一样，则继续判断第二个字母。
 * <span class="hljs-doctag">@params</span>: string
 * <span class="hljs-doctag">@retrun</span>: string[]
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">whatDay</span>(<span class="hljs-params">str:string</span>):string[] {
    <span class="hljs-keyword">let</span> weekDay=[
        {
            <span class="hljs-attr">en</span>:<span class="hljs-string">'monday'</span>,
            <span class="hljs-attr">zh</span>: <span class="hljs-string">'星期一'</span>
        },
        {
            <span class="hljs-attr">en</span>:<span class="hljs-string">'tuesday'</span>,
            <span class="hljs-attr">zh</span>: <span class="hljs-string">'星期二'</span>
        },
        {
            <span class="hljs-attr">en</span>:<span class="hljs-string">'wednesday'</span>,
            <span class="hljs-attr">zh</span>: <span class="hljs-string">'星期三'</span>
        },
        {
            <span class="hljs-attr">en</span>:<span class="hljs-string">'thursday'</span>,
            <span class="hljs-attr">zh</span>: <span class="hljs-string">'星期四'</span>
        },
        {
            <span class="hljs-attr">en</span>:<span class="hljs-string">'friday'</span>,
            <span class="hljs-attr">zh</span>: <span class="hljs-string">'星期五'</span>
        },
        {
            <span class="hljs-attr">en</span>:<span class="hljs-string">'saturday'</span>,
            <span class="hljs-attr">zh</span>: <span class="hljs-string">'星期六'</span>
        },
        {
            <span class="hljs-attr">en</span>:<span class="hljs-string">'sunday'</span>,
            <span class="hljs-attr">zh</span>: <span class="hljs-string">'星期日'</span>
        }
    ]
    <span class="hljs-keyword">let</span> <span class="hljs-attr">days</span>:string[]=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;weekDay.<span class="hljs-property">length</span>;i++){
        <span class="hljs-comment">// 按照题目是想一个字母一个字母的判断，但是其实我们直接判断输入的字符串是不是前几个字母会更简单些</span>
        <span class="hljs-keyword">if</span>(weekDay[i].<span class="hljs-property">en</span>.<span class="hljs-title function_">indexOf</span>(str.<span class="hljs-title function_">toLowerCase</span>())===<span class="hljs-number">0</span>){
            days.<span class="hljs-title function_">push</span>(weekDay[i].<span class="hljs-property">zh</span>)
        }
    }
    <span class="hljs-keyword">return</span> days
}


<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 测试，我们这里偷了个懒，用传入whatDay函数的字符代替了输入的字符，打印出对应的判断结果
 * <span class="hljs-doctag">@params</span>: string
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">str</span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> tmp=<span class="hljs-title function_">whatDay</span>(str);
    <span class="hljs-keyword">if</span>(tmp.<span class="hljs-property">length</span>===<span class="hljs-number">0</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'你输入的字符找不到对应的星期几哟'</span>)
    }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(tmp.<span class="hljs-property">length</span>&gt;<span class="hljs-number">1</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'你输入的字符找到多个对应的星期哟，还需要继续输入哟'</span>)
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`答案是：<span class="hljs-subst">${tmp[<span class="hljs-number">0</span>]}</span>`</span>)
    }
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">test</span>(<span class="hljs-string">'s'</span>)
<span class="hljs-comment">// 你输入的字符找到多个对应的星期哟，还需要继续输入哟</span>

<span class="hljs-title function_">test</span>(<span class="hljs-string">'sa'</span>)
<span class="hljs-comment">// 答案是：星期六</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：输出9*9口诀乘法表"><a href="#问题：输出9*9口诀乘法表">问题：输出9*9口诀乘法表</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 输出9*9口诀乘法表
 * <span class="hljs-doctag">@params</span>: null
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">printMultiTable</span>(<span class="hljs-params"></span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">1</span>;i&lt;<span class="hljs-number">10</span>;i++){
        <span class="hljs-keyword">let</span> rowStr=<span class="hljs-string">''</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> j=<span class="hljs-number">1</span>;j&lt;=i;j++){
            rowStr += <span class="hljs-string">`<span class="hljs-subst">${i}</span>*<span class="hljs-subst">${j}</span>=<span class="hljs-subst">${i*j}</span><span class="hljs-subst">${i*j&lt;<span class="hljs-number">10</span>?<span class="hljs-string">' '</span>:<span class="hljs-string">''</span>}</span>  `</span>
        }
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(rowStr)
    }
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">printMultiTable</span>()
<span class="hljs-comment">/*
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12  4*4=16  
5*1=5   5*2=10  5*3=15  5*4=20  5*5=25  
6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  
7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  
8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  
9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81 
*/</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：将一个数组逆序输出"><a href="#问题：将一个数组逆序输出">问题：将一个数组逆序输出</a></h3><p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 数组逆序
 * <span class="hljs-doctag">@params</span>: number[]
 * <span class="hljs-doctag">@retrun</span>: number[]
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">reverseArray</span>(<span class="hljs-params">arr:number[]</span>):number[] {
    <span class="hljs-keyword">let</span> _arr=arr.<span class="hljs-title function_">concat</span>()
    <span class="hljs-keyword">let</span> len=_arr.<span class="hljs-property">length</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;len/<span class="hljs-number">2</span>;i++){
        [_arr[i],_arr[len-i-<span class="hljs-number">1</span>]]=[_arr[len-i-<span class="hljs-number">1</span>],_arr[i]]
    }
    <span class="hljs-keyword">return</span> _arr
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 将一个数组逆序输出
 * <span class="hljs-doctag">@params</span>: number[]
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">arr:number[]</span>){
    <span class="hljs-keyword">let</span> _arr=<span class="hljs-title function_">reverseArray</span>(arr)
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${arr}</span> 逆序后结果是：<span class="hljs-subst">${_arr}</span>`</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">test</span>([<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>,<span class="hljs-number">8</span>,<span class="hljs-number">21</span>,<span class="hljs-number">12</span>,<span class="hljs-number">1</span>])
<span class="hljs-comment">// 1,3,6,8,21,12,1 逆序后结果是：1,12,21,8,6,3,1</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>js里面数组逆序可以直接用reverse 方法实现，我们这里其实相当于是自己实现了个reverse方法。。。</p>
<h3 id="问题：输入某年某月某日，判断这一天是这一年的第几天？"><a href="#问题：输入某年某月某日，判断这一天是这一年的第几天？">问题：输入某年某月某日，判断这一天是这一年的第几天？</a></h3><p>  tips:</p>
<h5 id="公历中的闰年是什么？"><a href="#公历中的闰年是什么？">公历中的闰年是什么？</a></h5><p>  闰年（Leap Year）是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。闰年共有366天，其中2月份有29天。</p>
<p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 根据传入的年、月、日，返回该日期是该年中的第多少天
 * <span class="hljs-doctag">@params</span>: (number) 年份
 * <span class="hljs-doctag">@params</span>: (number) 月份
 * <span class="hljs-doctag">@params</span>: (number) 月份中的天数
 * <span class="hljs-doctag">@retrun</span>: (number) 当年中的第多少天
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">getYearsDays</span>(<span class="hljs-params">year:number,month:number,day:number</span>):number{
    <span class="hljs-keyword">let</span> leapYearNum=year%<span class="hljs-number">4</span>===<span class="hljs-number">0</span>?<span class="hljs-number">1</span>:<span class="hljs-number">0</span>
    <span class="hljs-keyword">let</span> beforMonthDays=<span class="hljs-number">0</span>
    <span class="hljs-keyword">switch</span>(month){
        <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">31</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入1月份正确的天数，月份范围内天数1-31，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">0</span>
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">28</span>+leapYearNum){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入2月份正确的天数，月份范围内天数1-<span class="hljs-subst">${<span class="hljs-number">28</span>+leapYearNum}</span>，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">31</span>
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">31</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入3月份正确的天数，月份范围内天数1-31，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">59</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">4</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">30</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入4月份正确的天数，月份范围内天数1-30，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">90</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">5</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">31</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入5月份正确的天数，月份范围内天数1-31，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">120</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">6</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">30</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入6月份正确的天数，月份范围内天数1-30，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">151</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">7</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">31</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入7月份正确的天数，月份范围内天数1-31，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">181</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">8</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">31</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入8月份正确的天数，月份范围内天数1-31，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">212</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">9</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">30</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入9月份正确的天数，月份范围内天数1-30，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">243</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">10</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">31</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入10月份正确的天数，月份范围内天数1-31，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">273</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">11</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">30</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入11月份正确的天数，月份范围内天数1-30，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">304</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">12</span>:
            <span class="hljs-keyword">if</span>(day&lt;<span class="hljs-number">1</span> || day&gt;<span class="hljs-number">31</span>){
                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入12月份正确的天数，月份范围内天数1-31，您的天数是<span class="hljs-subst">${day}</span>`</span>)
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
            beforMonthDays=<span class="hljs-number">334</span>+leapYearNum
        <span class="hljs-keyword">break</span>
        <span class="hljs-attr">default</span>:
            <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`请传入月份正确的月份，月份范围是1-12，您的月份是<span class="hljs-subst">${month}</span>`</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
    }
    <span class="hljs-keyword">return</span> beforMonthDays+day
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 打印出传入的年、月、日是该年中的第多少天
 * <span class="hljs-doctag">@params</span>: (string) 2020年8月22日
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">printDays</span>(<span class="hljs-params">time:string</span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> _arr1=time.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/\D/g</span>,<span class="hljs-string">' '</span>).<span class="hljs-title function_">split</span>(<span class="hljs-string">' '</span>);
    <span class="hljs-keyword">let</span> <span class="hljs-attr">times</span>:number[]=[
        <span class="hljs-built_in">parseInt</span>(_arr1[<span class="hljs-number">0</span>]),
        <span class="hljs-built_in">parseInt</span>(_arr1[<span class="hljs-number">1</span>]),
        <span class="hljs-built_in">parseInt</span>(_arr1[<span class="hljs-number">2</span>])
    ]
    <span class="hljs-keyword">let</span> days=getYearsDays.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>,times)
    <span class="hljs-keyword">if</span>(days!==<span class="hljs-number">0</span>){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${time}</span> 这一天是这一年的第<span class="hljs-subst">${days}</span>天`</span>)
    }
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">printDays</span>(<span class="hljs-string">'2020年8月22日'</span>)
<span class="hljs-comment">// 2020年8月22日 这一天是这一年的第235天</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？"><strong>问题</strong>：有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？</h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-keyword">function</span> <span class="hljs-title function_">rabbitsComputed</span>(<span class="hljs-params">month:number</span>):number{
    <span class="hljs-keyword">if</span>(month &lt; <span class="hljs-number">1</span>){
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
    }
    <span class="hljs-keyword">if</span>(month &lt; <span class="hljs-number">3</span>){
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">a</span>:number=<span class="hljs-number">1</span>
    <span class="hljs-keyword">let</span> <span class="hljs-attr">b</span>:number=<span class="hljs-number">2</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">3</span>;i&lt;month;i++){
        [a,b]=[b,a+b]
    }
    <span class="hljs-keyword">return</span> a+b
}

<span class="hljs-keyword">function</span> <span class="hljs-title function_">printEveryMonthNumber</span>(<span class="hljs-params">endMonth:number</span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">1</span>;i&lt;endMonth;i++){
        <span class="hljs-keyword">let</span> <span class="hljs-attr">rabbitNum</span>:number=<span class="hljs-title function_">rabbitsComputed</span>(i)
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`第<span class="hljs-subst">${i}</span>月有<span class="hljs-subst">${rabbitNum}</span>对兔子`</span>)
    }
}

<span class="hljs-title function_">printEveryMonthNumber</span>(<span class="hljs-number">12</span>)

<span class="hljs-comment">// 输出结果</span>
第<span class="hljs-number">1</span>月有<span class="hljs-number">1</span>对兔子
第<span class="hljs-number">2</span>月有<span class="hljs-number">1</span>对兔子
第<span class="hljs-number">3</span>月有<span class="hljs-number">3</span>对兔子
第<span class="hljs-number">4</span>月有<span class="hljs-number">5</span>对兔子
第<span class="hljs-number">5</span>月有<span class="hljs-number">8</span>对兔子
第<span class="hljs-number">6</span>月有<span class="hljs-number">13</span>对兔子
第<span class="hljs-number">7</span>月有<span class="hljs-number">21</span>对兔子
第<span class="hljs-number">8</span>月有<span class="hljs-number">34</span>对兔子
第<span class="hljs-number">9</span>月有<span class="hljs-number">55</span>对兔子
第<span class="hljs-number">10</span>月有<span class="hljs-number">89</span>对兔子
第<span class="hljs-number">11</span>月有<span class="hljs-number">144</span>对兔子
第<span class="hljs-number">12</span>月有<span class="hljs-number">233</span>对兔子</span>
</code><span class="copy-button">复制代码</span></pre>
<p>我们知道，这个问题其实就是一个 斐波那契数列 ，从第三项开始，每一项等于前两项之和。。。上面的函数就是实现这个过程。。。</p>
<h3 id="问题：打印出如下图案（菱形）"><a href="#问题：打印出如下图案（菱形）">问题：打印出如下图案（菱形）</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 打印出菱形图案
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">diamondPattern</span>(<span class="hljs-params">maxRow:number=<span class="hljs-number">5</span>,space:number=<span class="hljs-number">1</span></span>):<span class="hljs-keyword">void</span>{
    <span class="hljs-keyword">let</span> col=maxRow * <span class="hljs-number">2</span>-<span class="hljs-number">1</span>;
    <span class="hljs-keyword">let</span> spaceStr=<span class="hljs-string">' '</span>
    <span class="hljs-keyword">let</span> flagStr=<span class="hljs-string">'*'</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=maxRow%(space*<span class="hljs-number">2</span>); i &lt;= col; i += <span class="hljs-number">2</span>*space){
        <span class="hljs-keyword">let</span> rowStr=spaceStr.<span class="hljs-title function_">repeat</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">abs</span>(maxRow - i) / <span class="hljs-number">2</span>) + flagStr.<span class="hljs-title function_">repeat</span>((i - maxRow) &gt; <span class="hljs-number">0</span> ? (col - i+<span class="hljs-number">1</span>) : i)
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(rowStr)
    }
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">diamondPattern</span>(<span class="hljs-number">5</span>)
  *
 ***
*****
 ***
  *


<span class="hljs-title function_">diamondPattern</span>(<span class="hljs-number">6</span>,<span class="hljs-number">2</span>)
  **
******
  **</span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：一个数如果恰好等于它的因子之和，这个数就称为完数。例如6=1＋2＋3.编程 找出1000以内的所有完数">问题：一个数如果恰好等于它的因子之和，这个数就称为"完数"。例如6=1＋2＋3.编程 找出1000以内的所有完数</h3><p>tips:</p>
<h5 id="什么是因子？">  什么是因子？</h5><p>假如整数n除以m，结果是无余数的整数，那么我们称m就是n的因子。 需要注意的是，唯有被除数，除数，商皆为整数，余数为零时，此关系才成立。反过来说，我们称n为m的倍数。</p>
<p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 找出小于参数的所有完数
 * <span class="hljs-doctag">@params</span>: (number) num
 * <span class="hljs-doctag">@retrun</span>: (array)
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">getPerfectNumber</span>(<span class="hljs-params">num:number</span>):number[] {
    <span class="hljs-keyword">let</span> <span class="hljs-attr">perfects</span>:number[]=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">2</span>;i&lt;num;i++){
        <span class="hljs-keyword">let</span> sum=<span class="hljs-number">0</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> j=<span class="hljs-number">1</span>;j&lt;i;j++){
            <span class="hljs-keyword">if</span>(i%j===<span class="hljs-number">0</span>){
                sum+=j
            }
        }
        <span class="hljs-keyword">if</span>(sum===i){
            perfects.<span class="hljs-title function_">push</span>(sum)
        }
    }
    <span class="hljs-keyword">return</span> perfects
}

<span class="hljs-keyword">function</span> <span class="hljs-title function_">printPerfects</span>(<span class="hljs-params">num:number</span>){
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${num}</span> 内的完数有：<span class="hljs-subst">${getPerfectNumber(<span class="hljs-number">1000</span>).join(<span class="hljs-string">','</span>)}</span>`</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">printPerfects</span>(<span class="hljs-number">1000</span>)
<span class="hljs-number">1000</span> 内的完数有：<span class="hljs-number">6</span>,<span class="hljs-number">28</span>,<span class="hljs-number">496</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：一个整数，它加上100后是一个完全平方数，再加上168又是一个完全平方数，请问该数是多少？"><a href="#问题：一个整数，它加上100后是一个完全平方数，再加上168又是一个完全平方数，请问该数是多少？">问题：一个整数，它加上100后是一个完全平方数，再加上168又是一个完全平方数，请问该数是多少？</a></h3><p>  tips:</p>
<h5 id="什么是完全平方数？"><a href="#什么是完全平方数？">什么是完全平方数？</a></h5><p>  完全平方指用一个整数乘以自己例如1<em>1，2</em>2，3*3等，依此类推。若一个数能表示成某个整数的平方的形式，则称这个数为完全平方数。完全平方数是非负数，而一个完全平方数的项有两个。</p>
<p>  <strong>思路：</strong><br>根据题目可以列出计算表达式：</p>
<p>设这个整数为x，那么就有：<br>x+100=n^2^<br>x+100+168=m^2^<br>即：m^2^-n^2^=168<br>即：(m-n)*(m+n)=168</p>
<p>那么我们将168分解为两个乘数相乘的方式来表达就是：<br>168<br>=1 * 168<br>=2 * 84<br>=3 * 56<br>=4 * 42<br>=6 * 28<br>=7 * 24<br>=8 * 21<br>=12 * 14</p>
<p>因为n+m和n-m的奇偶性是相同的，不可能一个奇数一个偶数，因此符号条件的表达式就只有：<br>2 * 84、4 * 42、6 * 28、12 * 14<br>如第一个表达式2 * 84，那么我们就可以表示为：<br>m-n=2，m+n=84，解出来，m=43，n=41<br>或：<br>m-n=84，m+n=2，解出来，m=43，n=-41<br>然而，不管n的正负，对于我们要计算出x的值来都没有影响，因为n取平方后都是一个非负整数</p>
<p>那么上面的计算过程用TypeScript 来实现，就是如下代码了：<br>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 将一个正整数分解成两个因数
 * <span class="hljs-doctag">@params</span>: (number) 自然数
 * <span class="hljs-doctag">@retrun</span>: (Array) 因数组成的数组
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">decompositionFactor</span>(<span class="hljs-params">num:number</span>):<span class="hljs-title class_">Array</span>&lt;<span class="hljs-title class_">Array</span>&lt;number&gt;&gt;{
    <span class="hljs-keyword">if</span>(num&lt;<span class="hljs-number">1</span> || <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">round</span>(num)!=num){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'参数必须是一个正整数'</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">primes</span>:<span class="hljs-title class_">Array</span>&lt;<span class="hljs-title class_">Array</span>&lt;number&gt;&gt;=[]
    <span class="hljs-keyword">let</span> maxPrime=num;
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">1</span>;i&lt;=num;i++){
        <span class="hljs-keyword">if</span>(i&gt;=maxPrime){
            <span class="hljs-keyword">break</span>
        }
        <span class="hljs-keyword">if</span>(num%i===<span class="hljs-number">0</span>){
            maxPrime=num/i
            primes.<span class="hljs-title function_">push</span>([i,maxPrime])
        }
    }
    <span class="hljs-keyword">return</span> primes
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 一个整数，它加上100后是一个完全平方数，再加上168又是一个完全平方数，求解该数
 * <span class="hljs-doctag">@params</span>: (number) 自然数
 * <span class="hljs-doctag">@params</span>: (number) 自然数
 * <span class="hljs-doctag">@retrun</span>: (Array) 解数组成的数组
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">separate</span>(<span class="hljs-params">addnum1:number,addnum2:number</span>):number[]{
    <span class="hljs-keyword">let</span> dePrimes=<span class="hljs-title function_">decompositionFactor</span>(addnum2)
    <span class="hljs-comment">// 过滤出奇、偶相同的因数</span>
    <span class="hljs-keyword">let</span> <span class="hljs-attr">multiDecom</span>:<span class="hljs-title class_">Array</span>&lt;<span class="hljs-title class_">Array</span>&lt;number&gt;&gt;=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;dePrimes.<span class="hljs-property">length</span>;i++){
        <span class="hljs-keyword">let</span> tmpArr=dePrimes[i]
        <span class="hljs-keyword">if</span>((tmpArr[<span class="hljs-number">0</span>]%<span class="hljs-number">2</span>===<span class="hljs-number">0</span> &amp;&amp; tmpArr[<span class="hljs-number">1</span>]%<span class="hljs-number">2</span>===<span class="hljs-number">0</span>) || tmpArr[<span class="hljs-number">0</span>]%<span class="hljs-number">2</span>!==<span class="hljs-number">0</span> &amp;&amp; tmpArr[<span class="hljs-number">1</span>]%<span class="hljs-number">2</span>!==<span class="hljs-number">0</span>){
            multiDecom.<span class="hljs-title function_">push</span>(tmpArr)
        }
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">_separate</span>:number[]=[]
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> j=<span class="hljs-number">0</span>;j&lt;multiDecom.<span class="hljs-property">length</span>;j++){
        <span class="hljs-keyword">let</span> mulTmpArr=multiDecom[j]
        _separate.<span class="hljs-title function_">push</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">pow</span>((mulTmpArr[<span class="hljs-number">0</span>]+mulTmpArr[<span class="hljs-number">1</span>])/<span class="hljs-number">2</span>,<span class="hljs-number">2</span>)-addnum2-addnum1)
    }
    <span class="hljs-keyword">return</span> _separate
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`一个整数，它加上100后是一个完全平方数，再加上168又是一个完全平方数，这个数可以是：<span class="hljs-subst">${separate(<span class="hljs-number">100</span>,<span class="hljs-number">168</span>).toString()}</span>`</span>)
<span class="hljs-comment">// 一个整数，它加上100后是一个完全平方数，再加上168又是一个完全平方数，这个数可以是：1581,261,21,-99</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：求一个3*3矩阵对角线元素之和"><a href="#问题：求一个3*3矩阵对角线元素之和">问题：求一个3*3矩阵对角线元素之和</a></h3><p>  实现代码:</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 求一个3*3矩阵对角线元素之和
 * <span class="hljs-doctag">@params</span>: Array&lt;Array&lt;number&gt;&gt;
 * <span class="hljs-doctag">@retrun</span>: number
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">sumDiagonal</span>(<span class="hljs-params">arr:<span class="hljs-built_in">Array</span>&lt;<span class="hljs-built_in">Array</span>&lt;number&gt;&gt;</span>):number {
    <span class="hljs-keyword">if</span> (!<span class="hljs-title class_">Array</span>.<span class="hljs-title function_">isArray</span>(arr)) {
        <span class="hljs-keyword">throw</span> <span class="hljs-title class_">TypeError</span>(<span class="hljs-string">"param must be a Array"</span>);
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">sum1</span>:number=<span class="hljs-number">0</span>
    <span class="hljs-keyword">let</span> <span class="hljs-attr">sum2</span>:number=<span class="hljs-number">0</span>
    <span class="hljs-keyword">let</span> arrLen=arr.<span class="hljs-property">length</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;arrLen;i++){
        sum1 += arr[i][i]
        sum2 += arr[i][arrLen-<span class="hljs-number">1</span>-i]
    }
    <span class="hljs-keyword">return</span> sum1+sum2
}

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 求一个3*3矩阵对角线元素之和
 * <span class="hljs-doctag">@params</span>: Array&lt;Array&lt;number&gt;&gt;
 * <span class="hljs-doctag">@retrun</span>: void
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">arr:<span class="hljs-built_in">Array</span>&lt;<span class="hljs-built_in">Array</span>&lt;number&gt;&gt;</span>){
    <span class="hljs-keyword">let</span> sum=<span class="hljs-title function_">sumDiagonal</span>(arr)
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`
    <span class="hljs-subst">${arr.map(item=&gt;{<span class="hljs-keyword">return</span> item[<span class="hljs-number">0</span>]})}</span>
    <span class="hljs-subst">${arr.map(item=&gt;{<span class="hljs-keyword">return</span> item[<span class="hljs-number">1</span>]})}</span>
    <span class="hljs-subst">${arr.map(item=&gt;{<span class="hljs-keyword">return</span> item[<span class="hljs-number">2</span>]})}</span>
    斜对角线和是：<span class="hljs-subst">${sum}</span>`</span>)
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-title function_">test</span>([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],[<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>],[<span class="hljs-number">7</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>]])

    <span class="hljs-number">1</span>,<span class="hljs-number">4</span>,<span class="hljs-number">7</span>
    <span class="hljs-number">2</span>,<span class="hljs-number">5</span>,<span class="hljs-number">8</span>
    <span class="hljs-number">3</span>,<span class="hljs-number">6</span>,<span class="hljs-number">9</span>
    斜对角线和是：<span class="hljs-number">30</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>我们可以看出来，求一个3*3矩阵对角线元素之和其实就是求一个二维数组的两条斜对角线之和。</p>
<h3 id="问题：给一个不多于5位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。"><a href="#问题：给一个不多于5位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。">问题：给一个不多于5位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。</a></h3><p>  实现代码，方式一：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 分解数字的每位数字并逆序打印返回
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: array
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">numberDecompose</span>(<span class="hljs-params">num:number=<span class="hljs-number">1</span></span>):number[]{
    <span class="hljs-keyword">if</span>(num&lt;<span class="hljs-number">1</span> &amp;&amp; <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>(num)!==num){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'请输入正整数'</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">return</span> num.
            <span class="hljs-title function_">toString</span>().
            <span class="hljs-title function_">split</span>(<span class="hljs-string">''</span>).
            <span class="hljs-title function_">reverse</span>().
            <span class="hljs-title function_">map</span>(<span class="hljs-function"><span class="hljs-params">item</span>=&gt;</span>{
                <span class="hljs-keyword">return</span> <span class="hljs-built_in">parseInt</span>(item)
            })
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">numberDecompose</span>(<span class="hljs-number">54321</span>).<span class="hljs-title function_">join</span>(<span class="hljs-string">' '</span>))
<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>  实现代码，方式二：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 分解数字的每位数字并逆序打印返回
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: array
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">numberDecompose</span>(<span class="hljs-params">num:number=<span class="hljs-number">1</span></span>):number[]{
    <span class="hljs-keyword">if</span>(num&lt;<span class="hljs-number">1</span> &amp;&amp; <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>(num)!==num){
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'请输入正整数'</span>)
        <span class="hljs-keyword">return</span> []
    }
    <span class="hljs-keyword">let</span> <span class="hljs-attr">tmp</span>:number[]=[]
    <span class="hljs-keyword">while</span>(num&gt;=<span class="hljs-number">1</span>){
        tmp.<span class="hljs-title function_">push</span>(num%<span class="hljs-number">10</span>)
        num=<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>(num/<span class="hljs-number">10</span>)
    }
    <span class="hljs-keyword">return</span> tmp
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">numberDecompose</span>(<span class="hljs-number">54321</span>).<span class="hljs-title function_">join</span>(<span class="hljs-string">' '</span>))
<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span></span>
</code><span class="copy-button">复制代码</span></pre>
<h3 id="问题：有5个人坐在一起，问第5个人多少岁？他说比第4个人大2岁。问第4个人岁数，他说比第3个人大2岁。问第3个人，又说比第2人大2岁。问第2个人，说比第1个人大2岁。最后问第1个人，他说是10岁。请问第5个人多大？"><a href="#问题：有5个人坐在一起，问第5个人多少岁？他说比第4个人大2岁。问第4个人岁数，他说比第3个人大2岁。问第3个人，又说比第2人大2岁。问第2个人，说比第1个人大2岁。最后问第1个人，他说是10岁。请问第5个人多大？">问题：有5个人坐在一起，问第5个人多少岁？他说比第4个人大2岁。问第4个人岁数，他说比第3个人大2岁。问第3个人，又说比第2人大2岁。问第2个人，说比第1个人大2岁。最后问第1个人，他说是10岁。请问第5个人多大？</a></h3><p>  实现代码：</p>
<pre><code class="language-JavaScript"><span class="code-block"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@Description</span>: 有5个人坐在一起，问第5个人多少岁？他说比第4个人大2岁。
 * 问第4个人岁数，他说比第3个人大2岁。问第3个人，又说比第2人大2岁。
 * 问第2个人，说比第1个人大2岁。最后问第1个人，他说是10岁。
 * 请问第5个人多大？
 * <span class="hljs-doctag">@params</span>: number
 * <span class="hljs-doctag">@retrun</span>: number
 */</span>
<span class="hljs-keyword">function</span> <span class="hljs-title function_">acquisitionAge</span>(<span class="hljs-params">num:number=<span class="hljs-number">1</span></span>):number{
    <span class="hljs-keyword">if</span>(num===<span class="hljs-number">1</span>){
        <span class="hljs-keyword">return</span> <span class="hljs-number">10</span>
    }
    <span class="hljs-keyword">return</span> <span class="hljs-title function_">acquisitionAge</span>(num-<span class="hljs-number">1</span>)+<span class="hljs-number">2</span>
}

<span class="hljs-comment">// 测试运行结果</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`第五个人的年龄是<span class="hljs-subst">${acquisitionAge(<span class="hljs-number">5</span>)}</span>岁`</span>)
<span class="hljs-comment">// 第五个人的年龄是18岁</span></span>
</code><span class="copy-button">复制代码</span></pre>
<p>参考资料：<br><a href="https://baike.baidu.com/item/%E8%B4%A8%E6%95%B0/263515?fromtitle=%E7%B4%A0%E6%95%B0&amp;fromid=115069&amp;fr=aladdin">https://baike.baidu.com/item/%E8%B4%A8%E6%95%B0/263515?fromtitle=%E7%B4%A0%E6%95%B0&amp;fromid=115069&amp;fr=aladdin</a></p>
<p><a href="https://baike.baidu.com/item/%E8%B4%A8%E6%95%B0/263515?fr=aladdin">https://baike.baidu.com/item/%E8%B4%A8%E6%95%B0/263515?fr=aladdin</a></p>
<p><a href="https://baike.baidu.com/item/%E6%B0%B4%E4%BB%99%E8%8A%B1%E6%95%B0/2746160?fr=aladdin">https://baike.baidu.com/item/%E6%B0%B4%E4%BB%99%E8%8A%B1%E6%95%B0/2746160?fr=aladdin</a></p>
<p><a href="https://baike.baidu.com/item/%E6%AD%A3%E6%95%B4%E6%95%B0/8461335?fr=aladdin">https://baike.baidu.com/item/%E6%AD%A3%E6%95%B4%E6%95%B0/8461335?fr=aladdin</a><br><a href="https://baike.baidu.com/item/%E8%B4%A8%E5%9B%A0%E6%95%B0/6192269?fr=aladdin">https://baike.baidu.com/item/%E8%B4%A8%E5%9B%A0%E6%95%B0/6192269?fr=aladdin</a></p>
<p><a href="https://baike.baidu.com/item/%E6%9C%80%E5%A4%A7%E5%85%AC%E7%BA%A6%E6%95%B0/869308?fr=aladdin">https://baike.baidu.com/item/%E6%9C%80%E5%A4%A7%E5%85%AC%E7%BA%A6%E6%95%B0/869308?fr=aladdin</a><br><a href="https://baike.baidu.com/item/%E6%9C%80%E5%B0%8F%E5%85%AC%E5%80%8D%E6%95%B0/6192375?fr=aladdin">https://baike.baidu.com/item/%E6%9C%80%E5%B0%8F%E5%85%AC%E5%80%8D%E6%95%B0/6192375?fr=aladdin</a></p>
<p><a href="https://baike.baidu.com/item/%E9%97%B0%E5%B9%B4/27098?fr=aladdin">https://baike.baidu.com/item/%E9%97%B0%E5%B9%B4/27098?fr=aladdin</a></p>
<p><a href="https://baike.baidu.com/item/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97/99145?fr=aladdin">https://baike.baidu.com/item/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97/99145?fr=aladdin</a></p>
<p><a href="https://baike.baidu.com/item/%E5%AE%8C%E5%85%A8%E5%B9%B3%E6%96%B9%E6%95%B0/8025061?fr=aladdin">https://baike.baidu.com/item/%E5%AE%8C%E5%85%A8%E5%B9%B3%E6%96%B9%E6%95%B0/8025061?fr=aladdin</a></p>
